package org.come.mouslisten;

import org.come.Frame.PawnJfram;
import org.come.Frame.ZhuFrame;
import org.come.bean.PawnGoodsMessBean;
import org.come.entity.Goodstable;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.GsonUtil;
import org.come.until.MessagrFlagUntil;

import java.awt.event.MouseEvent;

/**
 * 类说明
 *
 * @author 黄建彬
 * @date 2017年12月21日 新建
 *
 *
 *       确认典当按钮
 */

public class SurePawnMouslisten {
	// 用户选中的wupin
	private static Goodstable goodstable;
	/**
	 * 典当数量
	 */
	public static int getqian() {
		try {
			int x = Integer.parseInt(PawnJfram.getPawnJfram().getPawnjpanel().getGoodsCount().getText());
			if (x<=0) {
				return 0;
			}
			if (goodstable.getUsetime() < x) {
				x = goodstable.getUsetime();
			}
			return x;
		} catch (Exception e) {
			// TODO: handle exception
			return 0;
		}
	}
	public static Goodstable getGoodstable() {
		return goodstable;
	}
	public static void setGoodstable(Goodstable goodstable) {
		SurePawnMouslisten.goodstable = goodstable;
	}
	/**
	 * 典当物品的方法
	 */
	public static void surePawn() {
		if (goodstable == null) {
			ZhuFrame.getZhuJpanel().addPrompt2("没有选中的物品");
			PawnJfram.getPawnJfram().getPawnjpanel().qingchu();
			return;
		}
		if(goodstable.getGoodlock() == 1){
			ZhuFrame.getZhuJpanel().addPrompt("该物品已加锁，不可典当。。");
			return;
		}
		if(goodstable.getType() == 2255){
			ZhuFrame.getZhuJpanel().addPrompt("守护石不可典当。。");
			return;
		}
		if(GoodsListFromServerUntil.isExist(goodstable)){
			return;
		}
		// 典当的数量
		int count = getqian();
		if (count<=0||count>goodstable.getUsetime()) {
			PawnJfram.getPawnJfram().getPawnjpanel().qingchu();
			return;
		}
		// 返回给服务器的对象
		PawnGoodsMessBean goodsMessBean = new PawnGoodsMessBean();
		goodsMessBean.setRgid(goodstable.getRgid());//设置典当物品
		goodsMessBean.setNumber(count);//设置典当个数
		String mes = Agreement.getAgreement().pawnAgreement(GsonUtil.getGsonUtil().getgson().toJson(goodsMessBean));
		SendMessageUntil.toServer(mes);
		// 确认典当完清除面板
		goodstable.setUsetime(goodstable.getUsetime() - count);
		if (goodstable.getUsetime() <= 0) {
			GoodsListFromServerUntil.Deletebiaoid(goodstable.getRgid());
		}
		PawnJfram.getPawnJfram().getPawnjpanel().qingchu();
	}

}
